/*
author:
start_date:
version:
*/

//website:

/*
thought:
*/


#include <iostream>

using namespace std;

int gcdEX(int a,int & x,int b,int & y,int c);



int main(void) {

  const int M = 23 * 28 * 33;
  const int Mi[3] = {23,28,33};
  int m[3],t[3];
  m[0] = M/23;
  m[1] = M/28;
  m[2] = M/33;
  for (int a = 0; a < 3; a++) {
    int y;
    //t[a] = inverse(m[a],Mi[a]);
    gcdEX(m[a],t[a],Mi[a],y,1);
  }

  int p,e,i,d,res;
  cin >> p >> e >> i >> d;
  for (int j = 1;p!=-1 || e!=-1 || i!=-1 || d!=-1 ; j++)
 {
   res = p*t[0]*m[0] + e*t[1]*m[1] + i*t[2]*m[2];
   while (res - M > d || res <= d || res > 21252 + d) {
     if(res <= d)  res = res + M;
     if(res - M > d) res = res - M;
     if(res > 21252 + d) res = res - M;
   }

   std::cout << "Case "<< j <<": the next triple peak occurs in "<< res - d <<" days. " << '\n';
    cin >> p >> e >> i >> d;
  }




  return 0;
}


int gcdEX(int a,int & x,int b,int & y,int c)
{
  int r = 0;
  if(b == 0)
  {
    if(c % a == 0)
    {
       x = c/a;
       return 1;
     }
    else return 0;
  }
  else
  {
    int s,t;
    r = gcdEX(b,s,a%b,t,c);
    x = t,y = s-(a/b)*t;
  //  std::cout << x << " " << y << '\n';

  }
  return r;

}//1表示有解，0表示无解；
